package com.gzz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gzz.mapper.SceneryMapper;
import com.gzz.pojo.DefaultTicketNum;
import com.gzz.pojo.Ticket;
import com.gzz.mapper.TicketMapper;
import com.gzz.service.DefaultTicketNumService;
import com.gzz.service.TicketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周佳成
 * @since 2023-11-20
 */
@Service
public class TicketServiceImpl extends ServiceImpl<TicketMapper, Ticket> implements TicketService {
	@Resource
	SceneryMapper sceneryMapper;

	@Autowired
	TicketService ticketService;
	@Autowired
	DefaultTicketNumService defaultTicketNumService;
	@Override
	public List<Integer> getMapForDateAndNum(Long scId) {
		List<Integer> res = new ArrayList<>();

		List<Ticket> list;

		LambdaQueryWrapper<Ticket> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(Ticket::getScId,scId).orderByAsc(Ticket::getTkDate);

		list = list(lambdaQueryWrapper);

		for(Ticket ticket : list){
			res.add(ticket.getNum());
		}

		return res;

	}
	//每天23.55添加第七天的票
	@Scheduled(cron="0 55 23 * * *")
	@Override
	public void UpdateTicketEveryday() {
		List<Long> list = sceneryMapper.getAllId();
		for(Long id : list){
			Ticket ticket = new Ticket();
			DefaultTicketNum dn = defaultTicketNumService.getById(id);
			int num = dn.getNum();
			ticket.setNum(num);
			ticket.setScId(id);
			Long tid = (long)Math.abs(UUID.randomUUID().hashCode());
			ticket.setTicketId(tid);
			LocalDate localDate =LocalDate.now();
			//ticket.setTkDate(localDate.plusDays(7));
			Date d = Date.from(localDate.plusDays(7).atStartOfDay(ZoneId.of("Asia/Shanghai")).toInstant());
			ticket.setTkDate(d);
			ticketService.save(ticket);
		}
	}
}
